var Num = Vue.component('num', {
    data() {
        return {
            editStatus: true,       // 是否可编辑
            NumInfoData: [],        // 当前数据
            NumInfoOldData: [],    // 已经存在的旧数据
            name: "",               // 当前的类型名
            num: "",               // 当前的产品名
        }
    },

    template: `<div class="num__box">
                    <div class="num__con">
                        <h3 class="index__title num__title">款式数量</h3>
                        <div class="index__info num__info">
                            <div class="index__table__list num__table__list">
                                <table class="table__num">
                                    <tbody>
                                        <tr>
                                            <th>款式名称</th>
                                            <th>款式数量</th>
                                        </tr>
                                        <tr v-for="(item, index) in NumInfoData" :key="index" style="">
                                            <td>
                                                <span>{{item.name}}</span>
                                            </td>
                                            <td>
                                                <span>{{item.num || 0}}</span>
                                                <b v-show="!editStatus" @click="deleteInfo(index)">—</b>
                                            </td>
                                        </tr>
                                        <tr v-show="!editStatus">
                                            <td>
                                                <input placeholder="款式名称" v-model="name">
                                            </td>
                                            <td>
                                                <input type="number" placeholder="款式数量" v-model="num">
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                                <div class="index__table__add" v-show="!editStatus">
                                    <span @click="addNumeInfo">+</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="common__btn">
                        <div class="common__btn__box">
                            <span class="common__btn__sure" v-show="editStatus" @click="setEditFun">编辑</span>
                            <span class="common__btn__close" v-show="!editStatus" @click="setCancelFun">取消</span>
                            <span class="common__btn__sure" v-show="!editStatus" @click="updateNumInfo">保存设置</span>
                        </div>
                    </div>
                </div>`,

    methods: {
        setEditFun: function () {  // 编辑
            this.editStatus = !this.editStatus
        },
        setCancelFun: function () { // 取消
            var data = JSON.stringify(this.NumInfoOldData);
            this.NumInfoData = JSON.parse(data);
            this.editStatus = !this.editStatus;
        },
        getNumInfo() {
            // 获取款式数量
            var self = this;
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/company/com-design-list",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token')
                },
                success:function(res){
                    if(0 == res.code) {
                        var data = JSON.stringify(res.data);
                        self.NumInfoData = JSON.parse(data);
                        self.NumInfoOldData = JSON.parse(data);
                    }
                }
            })
        },
        addNumeInfo() {
            // 添加一行新的数据
            if(this.name == "" || this.num == "") {
                ELEMENT.Message.warning('数据不能为空');
                return;
            }
            this.NumInfoData.push({
                name: this.name,
                num: this.num
            });
            // 将数据清空
            this.name = "";
            this.num = "";
        },
        deleteInfo(_index) {
            // 删除当前这一行的数据
            var _data = this.NumInfoData.filter((item, index) => {
                if(_index != index) {
                    return item;
                }
            })
            this.NumInfoData = _data;
        },
        updateNumInfo() {
            // 保存信息
            var self = this;
            $.ajax({
                type:"POST",
                url: _UrlPrefix + "/api/member/company/com-design-set",
                data: {
                    app_access: _AppAccess,
                    token: localStorage.getItem('token'),
                    designs: JSON.stringify(self.NumInfoData)
                },
                success:function(res){
                    if(0 == res.code) {
                        ELEMENT.Message.success('保存数据成功');
                        self.editStatus = !self.editStatus;
                        var data = JSON.stringify(self.NumInfoData);
                        self.NumInfoOldData = JSON.parse(data);
                    }
                }
            })
        }
    },

    mounted() {
        
    },

    created() {
        // 获取款式数量
        this.getNumInfo();
    },
})